package com.tao.system.controller.sysUser;



import com.tao.common.creo.domain.Result;
import com.tao.system.domain.sysUser.dot.LoginDTO;
import com.tao.system.domain.sysUser.dot.sysUserSaveDTO;
import com.tao.system.domain.sysUser.vo.LoginUserVO;
import com.tao.system.service.sysUser.ISysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "管理员接口", description = "该组是关于管理员账户操作的接口")
@Slf4j
@RequestMapping("/sysUser")
@RestController
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;


    @PostMapping("/login")
    @Operation(summary = "管理员登录",description = "管理员登录接口")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "3103", description = "用户名或密码错误")
    public Result<String> login(@RequestBody LoginDTO loginDTO) {
        log.info("LoginDTO: " +loginDTO);
        return sysUserService.login(loginDTO.getUserAccount(), loginDTO.getPassword());
    }

    @RequestMapping("/add")
    @Operation(summary = "管理员注册", description = "管理员注册接口")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101", description = "用户已存在")
    public Result<String> add(@Validated @RequestBody sysUserSaveDTO sysUserSaveDTO) {
        return sysUserService.add(sysUserSaveDTO);
    }


    @GetMapping("/getUserInfo")
    public Result<LoginUserVO> getUserInfo(@RequestHeader(name = "Authorization") String token) {
        return sysUserService.getUserInfo(token);
    }

    /**
     * 退出登录, 删除token
     * @param token token
     * @return
     */
    @DeleteMapping("/logout")
    public Result<Boolean> logout(@RequestHeader(name = "Authorization") String token) {
        return sysUserService.logout(token);
    }
}
